package com.zxl.diy.weather.ui.activity;

import android.location.Location;
import android.os.Bundle;
import android.view.View;

import com.zxl.diy.weather.R;
import com.zxl.diy.weather.location.LocationHelper;
import com.zxl.diy.weather.location.OnLocationStatusListener;
import com.zxl.diy.weather.model.bean.City;
import com.zxl.diy.weather.model.observable.QueryCityInfoObservable;
import com.zxl.diy.weather.model.observable.QueryWeatherInfoObservable;
import com.zxl.manager.privacy.utils.base.BaseAppCompatActivity;
import com.zxl.manager.privacy.utils.model.Observer;
import com.zxl.manager.privacy.utils.utils.ClassUtils;
import com.zxl.manager.privacy.utils.utils.LogUtils;

import java.util.List;

public class MainActivity extends BaseAppCompatActivity implements OnLocationStatusListener, View.OnClickListener, Observer<City,Integer,List<City>> {

    private LocationHelper mLocationHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mLocationHelper = new LocationHelper(this, this);

        findViewById(R.id.location).setOnClickListener(this);

    }

    @Override
    public void onLocationFailed(int status) {
        LogUtils.d("onLocationFailed : " + status);
    }

    @Override
    public void onLocationSuccess(Location curLocation) {
        LogUtils.d("onLocationSuccess : " + curLocation.getLatitude() + ", " + curLocation.getLongitude());
        QueryCityInfoObservable.createQueryCityByLocation(curLocation).registerObserver(this).startModelHandle();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.location:
                mLocationHelper.startLocation();
                break;
        }
    }

    @Override
    public void onFail(Integer integer) {
        LogUtils.d("onFail : " + integer);
    }

    @Override
    public void onComplete(List<City> cities) {
        LogUtils.d("onComplete : " + (cities == null ? 0 : cities.size()));
    }

    @Override
    public void onSuccess(City city) {
        ClassUtils.toString(city.getClass(), city, "\\n");
        new QueryWeatherInfoObservable(false, city).startModelHandle();
    }
}
